"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.WebLayout = void 0;

var _react = _interopRequireWildcard(require("react"));

var _reactVirtualized = require("react-virtualized");

var _throttle = _interopRequireDefault(require("../../../utils/throttle"));

var _excluded = ["rowData", "height", "rowCount", "rowHeight", "onScroll", "width", "rowRenderer", "className", "onPullingUp", "isRowLoaded", "overscanRowCount", "lowerThreshold", "minHeight", "onScrollTop", "onScrollBottom", "renderBottom"];

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

var WebLayout = /*#__PURE__*/(0, _react.forwardRef)(function (_ref, ref) {
  var rowData = _ref.rowData,
      height = _ref.height,
      rowCount = _ref.rowCount,
      rowHeight = _ref.rowHeight,
      onScroll = _ref.onScroll,
      width = _ref.width,
      rowRenderer = _ref.rowRenderer,
      className = _ref.className,
      onPullingUp = _ref.onPullingUp,
      isRowLoaded = _ref.isRowLoaded,
      _ref$overscanRowCount = _ref.overscanRowCount,
      overscanRowCount = _ref$overscanRowCount === void 0 ? 1 : _ref$overscanRowCount,
      lowerThreshold = _ref.lowerThreshold,
      _ref$minHeight = _ref.minHeight,
      minHeight = _ref$minHeight === void 0 ? 50 : _ref$minHeight,
      onScrollTop = _ref.onScrollTop,
      onScrollBottom = _ref.onScrollBottom,
      renderBottom = _ref.renderBottom,
      props = _objectWithoutProperties(_ref, _excluded);

  // 偏移量
  var offset = (0, _react.useRef)(0);
  var cache = new _reactVirtualized.CellMeasurerCache({
    fixedWidth: true,
    minHeight: minHeight,
    show: false
  });

  var rendererRow = function rendererRow(_ref2) {
    var index = _ref2.index,
        key = _ref2.key,
        parent = _ref2.parent,
        style = _ref2.style,
        isScrolling = _ref2.isScrolling;
    return /*#__PURE__*/_react.default.createElement(_reactVirtualized.CellMeasurer, {
      cache: cache,
      columnIndex: 0,
      key: key,
      rowIndex: index,
      parent: parent
    }, function (_ref3) {
      var measure = _ref3.measure,
          registerChild = _ref3.registerChild;
      return /*#__PURE__*/_react.default.createElement("div", {
        ref: registerChild,
        style: style
      }, rowRenderer({
        index: index,
        data: rowData,
        measure: measure,
        style: style,
        isScrolling: isScrolling
      }), index === rowData.length - 1 && renderBottom);
    });
  };

  (0, _react.useImperativeHandle)(ref, function () {
    return {
      scrollTop: offset
    };
  }); // 处理滑动事件

  var handleScrollEvent = (0, _throttle.default)(function (_ref4) {
    var clientHeight = _ref4.clientHeight,
        scrollHeight = _ref4.scrollHeight,
        scrollTop = _ref4.scrollTop;

    if (typeof onScroll === 'function') {
      onScroll({
        clientHeight: clientHeight,
        scrollHeight: scrollHeight,
        scrollTop: scrollTop
      });
    }

    if (typeof onScrollTop === 'function' && scrollTop === 0) {
      onScrollTop({
        clientHeight: clientHeight,
        scrollHeight: scrollHeight,
        scrollTop: scrollTop
      });
    }

    if (typeof onScrollBottom === 'function' && clientHeight + scrollTop === scrollHeight) {
      onScrollBottom({
        clientHeight: clientHeight,
        scrollHeight: scrollHeight,
        scrollTop: scrollTop
      });
    } // 存储偏移量，以供外部访问


    offset.current = scrollTop;
  }, 16);
  return /*#__PURE__*/_react.default.createElement(_reactVirtualized.InfiniteLoader, {
    rowCount: rowCount,
    isRowLoaded: isRowLoaded,
    loadMoreRows: onPullingUp,
    threshold: lowerThreshold
  }, function (_ref5) {
    var onRowsRendered = _ref5.onRowsRendered,
        registerChild = _ref5.registerChild;
    return /*#__PURE__*/_react.default.createElement(_reactVirtualized.AutoSizer, {
      className: className
    }, function (_ref6) {
      var height = _ref6.height,
          width = _ref6.width;
      return /*#__PURE__*/_react.default.createElement(_reactVirtualized.List, _extends({
        className: className,
        ref: registerChild,
        onScroll: handleScrollEvent,
        deferredMeasurementCache: cache,
        height: height,
        overscanRowCount: overscanRowCount,
        onRowsRendered: onRowsRendered,
        rowCount: rowCount,
        rowHeight: cache.rowHeight,
        rowRenderer: rendererRow,
        width: width
      }, props));
    });
  });
});
exports.WebLayout = WebLayout;